home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’96 / Sessions ’96 / AI and The Mac / Demos / TravSales.Think / Main.cp < prev    next >
Encoding:
Text File  |  1996-06-22  |  2.5 KB  |  124 lines  |  [TEXT/MPS ]

  1.  
  2. //============================================================
  3. #ifndef _MYINDIVIDUAL_
  4.     #include "MyIndividual.h"
  5. #endif
  6.  
  7. #ifndef _POPULATION_
  8.     #include "Population.h"
  9. #endif
  10.  
  11. #ifndef _CIRCLECITYMAP_
  12.     #include "CircleCityMap.h"
  13. #endif
  14.  
  15. #ifndef _PATHGRAPH_
  16.     #include "PathGraph.h"
  17. #endif
  18.  
  19. #ifndef __DIALOGS__
  20.     #include <Dialogs.h>
  21. #endif
  22.  
  23. #ifndef __EVENTS__
  24.     #include <Events.h>
  25. #endif
  26.  
  27. #ifndef __FONTS__
  28.     #include <Fonts.h>
  29. #endif
  30.  
  31. /*
  32. #ifndef __MENUS__
  33.     #include <Menus.h>
  34. #endif
  35. */
  36.  
  37. #ifndef __QUICKDRAW__
  38.     #include <Quickdraw.h>
  39. #endif
  40.  
  41. #ifndef __WINDOWS__
  42.     #include <Windows.h>
  43. #endif
  44.  
  45. #ifndef __PACKAGES__
  46.     #include <Packages.h>
  47. #endif
  48.  
  49. #ifndef __TYPES__
  50.     #include <Types.h>
  51. #endif
  52.  
  53. #ifndef _GAFAILURE_
  54.     #include "GAFailure.h"
  55. #endif
  56.  
  57. extern GrafPtr thePort;        // for Random()
  58.  
  59.  
  60. //============================================================
  61. void
  62. main()
  63. {
  64.     short        numberIndividuals     = 200;
  65.     short        numberGenerations     = 25;
  66.     float        mutationRate        = .01;
  67.     float        crossoverRate        = .6;
  68.     int            numberCities        = 16;
  69.     
  70.     InitGraf( &thePort );
  71.     InitFonts();
  72.     InitWindows();
  73. //    InitMenus();
  74.     TEInit();
  75.     InitDialogs( NULL );
  76.     InitCursor();
  77.  
  78.     TCityMap* cityMap        = new TCircleCityMap( numberCities );
  79.     FailNIL(cityMap);
  80.     TMyIndividual* prototype    = new TMyIndividual( cityMap );
  81.     FailNIL(prototype);
  82.     TPopulation    pop( numberIndividuals, mutationRate, crossoverRate, prototype);
  83.     
  84.     WindowPtr    theWindow;
  85.     Rect        rBounds = { 40, 40, 400, 400 };
  86.     theWindow = NewWindow( NULL, &rBounds, "\pPlotting Window", TRUE, documentProc,
  87.                                                             (WindowPtr)-1L, TRUE, 0L );
  88.     
  89.     TPathGraph* pathGraph        = new TPathGraph ( *cityMap, theWindow, theWindow->portRect );
  90.     FailNIL(pathGraph);
  91.     SetPort( theWindow );
  92.     pathGraph->PlotCityLocations();
  93.     
  94.     EventRecord        event;
  95.     unsigned char    numText[16];
  96.     short* importanceArray = new short[ numberCities ];
  97.     FailNIL(importanceArray);
  98.     //TIndividual& bestIndividual    = pop.GetBestIndividual();
  99.     TIndividual* bestIndividual    = &(pop.GetBestIndividual());
  100.     for ( short generation = 0; generation < numberGenerations; ++generation )
  101.     {
  102.         GetNextEvent( everyEvent, &event );
  103.         pop.NextGeneration();
  104.         EraseRect( &theWindow->portRect );
  105.         pathGraph->PlotCityLocations();
  106.         bestIndividual    = &(pop.GetBestIndividual());
  107.         for (short geneIndex = 0; geneIndex < numberCities; ++geneIndex )
  108.             importanceArray[ geneIndex ] = bestIndividual->GetGene( geneIndex ).GetValue();
  109.         pathGraph->PlotRoute( importanceArray );
  110.     }
  111.  
  112.     while( !Button() )
  113.         GetNextEvent( everyEvent, &event );
  114.     while( Button() )
  115.         GetNextEvent( everyEvent, &event );
  116.  
  117.     DisposeWindow( theWindow );
  118.  
  119.  
  120.     
  121. }
  122.  
  123.  
  124.